/*
 * Copyright (C) 2001 Jacek Sieka, j_s@telia.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "BandwidthLimitPage.h"
#include "../client/SettingsManager.h"
#include "WinUtil.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

PropPage::Item BandwidthLimitPage::items[] = {
	{ IDC_THROTTLE_ENABLE, SettingsManager::THROTTLE_ENABLE, PropPage::T_BOOL },
	{ IDC_MAXDOWNLOAD, SettingsManager::MAX_DOWNLOAD_SPEED_REAL, PropPage::T_INT },
	{ IDC_MAXUPLOAD, SettingsManager::MAX_UPLOAD_SPEED, PropPage::T_INT },
	{ IDC_TIME_BASED_BW_LIMITING, SettingsManager::TIME_DEPENDENT_THROTTLE, PropPage::T_BOOL },
	{ IDC_MAXDOWNLOAD_TIME, SettingsManager::MAX_DOWNLOAD_SPEED_REAL_TIME, PropPage::T_INT },
	{ IDC_MAXUPLOAD_TIME, SettingsManager::MAX_UPLOAD_SPEED_TIME, PropPage::T_INT },
	{ IDC_BW_START_TIME, SettingsManager::BANDWIDTH_LIMIT_START, PropPage::T_INT },
	{ IDC_BW_END_TIME, SettingsManager::BANDWIDTH_LIMIT_END, PropPage::T_INT },
	{ IDC_UPLOADSLOTS_TIME, SettingsManager::WATCH_THE_DUCKLINGS_HOP_HOP_SKIP, PropPage::T_INT_SHOWDEFAULT },
	// pothead starts (czdc code) svn125
	{ IDC_DOWNLOAD_LIMIT_ENABLE, SettingsManager::DOWNLOAD_LIMIT_ENABLE, PropPage::T_BOOL },
	{ IDC_I_DOWN_SPEED, SettingsManager::MIN_DOWN_SPEED, PropPage::T_INT },
	{ IDC_TIME_DOWN, SettingsManager::DOWN_TIME, PropPage::T_INT },
	{ IDC_H_DOWN_SPEED, SettingsManager::H_DOWN_SPEED, PropPage::T_INT },
	{ IDC_MIN_FILE_SIZE, SettingsManager::MIN_FILE_SIZE, PropPage::T_INT },
	{ IDC_NUM_ONLINE_USERS, SettingsManager::NUM_ONLINE_USERS, PropPage::T_INT },
	{ IDC_REMOVE_IF_BELOW, SettingsManager::REMOVE_SLOW_USER, PropPage::T_INT },
	// pothead ends
	// CDM EXTENSION BEGINS
	{ IDC_RATIO, SettingsManager::DOWNLOAD_UPLOAD_RATIO, PropPage::T_INT_SHOWDEFAULT },
	{ IDC_RATIO_TIME, SettingsManager::DOWNLOAD_UPLOAD_RATIO_TIME, PropPage::T_INT_SHOWDEFAULT },
	// CDM EXTENSION ENDS
	{ 0, 0, PropPage::T_END }
};

LRESULT BandwidthLimitPage::onInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) {
	PropPage::read((HWND)*this, items);

	CUpDownCtrl spin;
	spin.Attach(GetDlgItem(IDC_UPLOADSPEEDSPIN));
	spin.SetRange32(0, 999);
	spin.Detach();
	spin.Attach(GetDlgItem(IDC_DOWNLOADSPEEDSPIN));
	spin.SetRange32(0, 999);
	spin.Detach();
	spin.Attach(GetDlgItem(IDC_UPLOADSPEEDSPIN_TIME));
	spin.SetRange32(0, 999);
	spin.Detach();
	spin.Attach(GetDlgItem(IDC_DOWNLOADSPEEDSPIN_TIME));
	spin.SetRange32(0, 999);
	spin.Detach();
	spin.Attach(GetDlgItem(IDC_UPLOADSLOTSSPIN_TIME));
	spin.SetRange32(1, 100);
	spin.Detach();
	// CDM EXTENSION BEGINS
	spin.Attach(GetDlgItem(IDC_RATIOSPIN));
	spin.SetRange32(1, 10);
	spin.Detach();
	spin.Attach(GetDlgItem(IDC_RATIOSPIN_TIME));
	spin.SetRange32(1, 10);
	spin.Detach();
	// CDM EXTENSION ENDS
	// pothead starts (czdc code) svn125
	spin.Attach(GetDlgItem(IDC_I_DOWN_SPEED_SPIN));
	spin.SetRange32(0, 99999);
	spin.Detach();
	spin.Attach(GetDlgItem(IDC_TIME_DOWN_SPIN));
	spin.SetRange32(0, 99999);
	spin.Detach();
	spin.Attach(GetDlgItem(IDC_H_DOWN_SPEED_SPIN));
	spin.SetRange32(0, 99999);
	spin.Detach();
	spin.Attach(GetDlgItem(IDC_MIN_FILE_SIZE_SPIN));
	spin.SetRange32(0, 99999);
	spin.Detach();
	spin.Attach(GetDlgItem(IDC_NUM_ONLINE_USERS_SPIN));
	spin.SetRange32(0, 99999);
	spin.Detach();
	spin.Attach(GetDlgItem(IDC_REMOVE_IF_BELOW_SPIN));
	spin.SetRange32(0, 99999);
	spin.Detach();
	// pothead ends

	timeCtrlBegin.Attach(GetDlgItem(IDC_BW_START_TIME));
	timeCtrlEnd.Attach(GetDlgItem(IDC_BW_END_TIME));

	timeCtrlBegin.AddString(CTSTRING(MIDNIGHT));
	timeCtrlEnd.AddString(CTSTRING(MIDNIGHT));
	for (int i = 1; i < 12; ++i) {
		timeCtrlBegin.AddString((Util::toString(i) + _T(" ") + CTSTRING(AM)).c_str());
		timeCtrlEnd.AddString((Util::toString(i) + _T(" ") + CTSTRING(AM)).c_str());
	}
	timeCtrlBegin.AddString(CTSTRING(NOON));
	timeCtrlEnd.AddString(CTSTRING(NOON));
	for (int i = 1; i < 12; ++i) {
		timeCtrlBegin.AddString((Util::toString(i) + _T(" ") + CTSTRING(PM)).c_str());
		timeCtrlEnd.AddString((Util::toString(i) + _T(" ") + CTSTRING(PM)).c_str());
	}

	timeCtrlBegin.SetCurSel(SETTING(BANDWIDTH_LIMIT_START));
	timeCtrlEnd.SetCurSel(SETTING(BANDWIDTH_LIMIT_END));

	timeCtrlBegin.Detach();
	timeCtrlEnd.Detach();

	setControlState();

	return TRUE;
}

LRESULT BandwidthLimitPage::onControlChecked(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	switch (wID) {
		case IDC_TIME_BASED_BW_LIMITING:
		case IDC_THROTTLE_ENABLE:
		case IDC_DOWNLOAD_LIMIT_ENABLE:
			setControlState();
			break;
		default:
			break;
	}
	return 0;
}

void BandwidthLimitPage::setControlState() {
	bool stateTime = (IsDlgButtonChecked(IDC_TIME_BASED_BW_LIMITING) != 0),
		stateEnabled = (IsDlgButtonChecked(IDC_THROTTLE_ENABLE) != 0);
	::EnableWindow(GetDlgItem(IDC_MAXUPLOAD), stateEnabled);
	::EnableWindow(GetDlgItem(IDC_UPLOADSPEEDSPIN), stateEnabled);
	::EnableWindow(GetDlgItem(IDC_MAXDOWNLOAD), stateEnabled);
	::EnableWindow(GetDlgItem(IDC_DOWNLOADSPEEDSPIN), stateEnabled);
	::EnableWindow(GetDlgItem(IDC_TIME_BASED_BW_LIMITING), stateEnabled);
	::EnableWindow(GetDlgItem(IDC_BW_START_TIME), stateEnabled && stateTime);
	::EnableWindow(GetDlgItem(IDC_BW_END_TIME), stateEnabled && stateTime);
	::EnableWindow(GetDlgItem(IDC_MAXUPLOAD_TIME), stateEnabled && stateTime);
	::EnableWindow(GetDlgItem(IDC_UPLOADSPEEDSPIN_TIME), stateEnabled && stateTime);
	::EnableWindow(GetDlgItem(IDC_MAXDOWNLOAD_TIME), stateEnabled && stateTime);
	::EnableWindow(GetDlgItem(IDC_DOWNLOADSPEEDSPIN_TIME), stateEnabled && stateTime);
	::EnableWindow(GetDlgItem(IDC_UPLOADSLOTS_TIME), stateEnabled && stateTime);
	::EnableWindow(GetDlgItem(IDC_UPLOADSLOTSSPIN_TIME), stateEnabled && stateTime);
	// CDM EXTENSION BEGINS
	::EnableWindow(GetDlgItem(IDC_RATIO), stateEnabled);
	::EnableWindow(GetDlgItem(IDC_RATIOSPIN), stateEnabled);
	::EnableWindow(GetDlgItem(IDC_RATIO_TIME), stateEnabled && stateTime);
	::EnableWindow(GetDlgItem(IDC_RATIOSPIN_TIME), stateEnabled && stateTime);
	// CDM EXTENSION ENDS
	// Pothead starts (czdc code) svn125
	bool state = (IsDlgButtonChecked(IDC_DOWNLOAD_LIMIT_ENABLE) != 0);
	::EnableWindow(GetDlgItem(IDC_I_DOWN_SPEED), state);
	::EnableWindow(GetDlgItem(IDC_I_DOWN_SPEED_SPIN), state);
	::EnableWindow(GetDlgItem(IDC_TIME_DOWN), state);
	::EnableWindow(GetDlgItem(IDC_TIME_DOWN_SPIN), state);
	::EnableWindow(GetDlgItem(IDC_H_DOWN_SPEED), state);
	::EnableWindow(GetDlgItem(IDC_H_DOWN_SPEED_SPIN), state);
	::EnableWindow(GetDlgItem(IDC_MIN_FILE_SIZE), state);
	::EnableWindow(GetDlgItem(IDC_MIN_FILE_SIZE_SPIN), state);
	::EnableWindow(GetDlgItem(IDC_NUM_ONLINE_USERS), state);
	::EnableWindow(GetDlgItem(IDC_NUM_ONLINE_USERS_SPIN), state);
	::EnableWindow(GetDlgItem(IDC_REMOVE_IF_BELOW), state);
	::EnableWindow(GetDlgItem(IDC_REMOVE_IF_BELOW_SPIN), state);	
	// Pothead ends
}
void BandwidthLimitPage::write() {
	PropPage::write((HWND)*this, items);

	// Do specialized writing here
	// settings->set(XX, YY);

	if (SETTING(MAX_DOWNLOAD_SPEED_REAL) < 0) settings->set(SettingsManager::MAX_DOWNLOAD_SPEED_REAL, 0);

	timeCtrlBegin.Attach(GetDlgItem(IDC_BW_START_TIME));
	timeCtrlEnd.Attach(GetDlgItem(IDC_BW_END_TIME));
	settings->set(SettingsManager::BANDWIDTH_LIMIT_START, timeCtrlBegin.GetCurSel());
	settings->set(SettingsManager::BANDWIDTH_LIMIT_END, timeCtrlEnd.GetCurSel());
	timeCtrlBegin.Detach();
	timeCtrlEnd.Detach();
}

LRESULT BandwidthLimitPage::onHelpInfo(LPNMHDR /*pnmh*/) {
	HtmlHelp(m_hWnd, WinUtil::getHelpFile().c_str(), HH_HELP_CONTEXT, IDD_BANDWIDTHPAGE);
	return 0;
}

/**
 * @file
 * $Id: BandwidthLimitPage.cpp 306 2005-08-20 15:49:52Z Pothead $
 */